package cn.neusoft.utils;

import org.apache.commons.beanutils.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
数据转换器
进行日期转换
自定义的日期转换器
 */
public class DateConverter implements Converter {
    //传递时间样式
    private String pattern;

    public DateConverter(String pattern) {
        this.pattern = pattern;
        if (pattern==null){
            this.pattern = "yyyy-MM-dd";
        }
    }

    @Override
    public Object convert(Class aClass, Object o) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        try {
            Date date = format.parse(o.toString());
            return date;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }
}
